﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using Jy123.Web.AppCode;

namespace Jy123.Web.home
{
    public partial class BlogList_My : System.Web.UI.Page
    {
        private int userID ;//暂用用户ID，写死，到时候用COOKIE替换过来

        protected List<int?> count;
        protected void Page_Load(object sender, EventArgs e)
        {
            int currentpage = 1;//当前页
            int pagesize = 4;//页记录大小

            //把被查询页赋值给当前页
            if (!string.IsNullOrEmpty(Request.QueryString["page"]))
            {
                currentpage = Convert.ToInt32(Request.QueryString["page"]);
            }

            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

            string strUserAuthorID = Request.QueryString["userID"];
            if (!string.IsNullOrEmpty(strUserAuthorID))
            {
                userID = Convert.ToInt32(strUserAuthorID);
            }

            StringBuilder indexHtml = new StringBuilder(Cache["user_bloglist_my"].ToString());
            //头部、底部、左侧用户命令菜单
            indexHtml = indexHtml.Replace("$Header$", Cache["user_header2"].ToString());
            indexHtml = indexHtml.Replace("$Footer$", Cache["user_footer"].ToString());
            indexHtml = indexHtml.Replace("$LeftUserCmd$", Cache["user_navigation_down"].ToString());
            indexHtml = indexHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());

            //统计站内信数量
            Jy123.BLL.User.Letter bllLetter = new Jy123.BLL.User.Letter();
            int newLetterCount = bllLetter.CountNewLetter(userID);
            //BLL.User_LetterInbox bllInbox = new Jy123.BLL.User_LetterInbox();
            //bllInbox.GetList("FUser = '" + userID + "'");
            indexHtml = indexHtml.Replace("$NewMsg$", newLetterCount.ToString());
            
            //我的日志列表
            Jy123.BLL.User.Blog bllUserBlog = new Jy123.BLL.User.Blog();
            DataTable dtMyBlog = bllUserBlog.BlogList_My(currentpage, pagesize, userID, out count).Tables[0];
            StringBuilder sbBlogLit = new StringBuilder();
            for (int i = 0; i < dtMyBlog.Rows.Count; i++)
            {
                string strHtml = Cache["user_bloglist_my_ul"].ToString();
                strHtml = strHtml.Replace("$ID$",dtMyBlog.Rows[i]["ID"].ToString());
                strHtml = strHtml.Replace("$Title$",dtMyBlog.Rows[i]["Title"].ToString());
                strHtml = strHtml.Replace("$PostTime$",dtMyBlog.Rows[i]["PostTime"].ToString());
                sbBlogLit.Append(strHtml);
            }
            string pageHtml = @"
                            <li class=""page"">
                                <span>
                                       {0}
                                </span>
                                <b>第{1}-{2}个/共{3}个</b>
                            </li>";
            string strHref = "BlogList_My.aspx?page=$num$";
            MoonPage mp = new MoonPage(false, true);
            mp.Count = Convert.ToInt32(count[1]);
            sbBlogLit.AppendFormat(pageHtml, mp.GetStrPage(currentpage, "no", "sel", strHref), 1 + (currentpage - 1) * pagesize, ((currentpage * pagesize) > count[0]) ? count[0] : currentpage * pagesize, count[0]);
            

            indexHtml.Replace("$BlogList_My_ul$",sbBlogLit.ToString());

            if (!string.IsNullOrEmpty(strUserAuthorID))
            {
                indexHtml.Replace("<input type='submit' class='input_btn pinfo' value='删除' id='submit' name='submit' />",string.Empty);
                indexHtml.Replace("我的日志",new Jy123.BLL.User_Main().GetModel(userID).UserName+"的日志");
            }

            Response.Write(indexHtml.ToString());
        }
    }
}
